/**
 * Copyright 2018-2020 stylefeng & fengshuonan (https://gitee.com/stylefeng)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.authstr.auth.service.imip;

import com.authstr.auth.mapper.inter.MenuDao;
import com.authstr.auth.mapper.inter.RoleDao;
import com.authstr.auth.service.inter.UserAuthService;
import com.authstr.auth.service.inter.UserService;
import com.authstr.auth.shiro.ShiroUser;
import com.authstr.basic.util.SpringUtils;
import com.authstr.model.base.BaseUser;
import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
//要求首先把上下文支持组件注册到spring
@DependsOn("springUtils")
public class UserAuthServiceImpl implements UserAuthService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private MenuDao menuDao;

//    @Autowired
//    private MenuMapper menuMapper;

    @Autowired
    private UserService userService;


    /**
     * 获取自身实例对象
     * @return
     */
    public static UserAuthService me() {
        return SpringUtils.getBean(UserAuthService.class);
    }

    @Override
    public BaseUser user(String account) {
        BaseUser user = userService.getByAccount(account);
        // 账号不存在
        if (null == user) {
            throw new CredentialsException();
        }
//        // 账号被冻结
//        if (!user.getStatus().equals(ManagerStatus.OK.getCode())) {
//            throw new LockedAccountException();
//        }
        return user;
    }

    @Override
    public ShiroUser shiroUser(BaseUser user) {
        ShiroUser shiroUser = createShiroUser(user);
        List<Map<String,Object>> roleInfo=roleDao.getRoleIdAndNameByUserId(user.getId());
        //用户的角色id集合
        List<Long> roleIds=new ArrayList<>();
        //用户的角色名称集合
        List<String> roleNames=new ArrayList<>();
        for(int i=0;i<roleInfo.size();i++){
            roleIds.add((Long)roleInfo.get(i).get("id"));
            roleNames.add((String)roleInfo.get(i).get("name"));
        }
        shiroUser.setRoleList(roleIds);
        shiroUser.setRoleNames(roleNames);

        return shiroUser;
    }

    @Override
    public List<String> getMenuUrlByRoleId(Long roleId) {
        return menuDao.getMenuUrlByRoleId(roleId);
    }

    @Override
    public String findRoleNameByRoleId(Long roleId) {
        return null;
    }

    @Override
    public SimpleAuthenticationInfo info(ShiroUser shiroUser, BaseUser user, String realmName) {
        // 密码加盐处理
        String salt = user.getSalt();
        String credentials = user.getPassword();
        ByteSource credentialsSalt = new Md5Hash(salt);
        return new SimpleAuthenticationInfo(shiroUser, credentials, credentialsSalt, realmName);
    }


    /**
     * 通过用户表的信息创建一个shiroUser对象
     */
    public static ShiroUser createShiroUser(BaseUser user) {
        ShiroUser shiroUser = new ShiroUser();
        if (user == null) {
            return shiroUser;
        }
        shiroUser.setId(user.getId());
        shiroUser.setAccount(user.getAccount());
        return shiroUser;
    }

}
